var path = require('path');
var node_modules_dir = path.join(__dirname, 'node_modules');
var webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
const SOURCE_DIR = './src';
const PRODUCT_DIR = 'dist';
const ENTRY_FILE_NAME = 'index.js';
const LIBRARY_NAME = 'UcsmyUI';

var common_loaders = [{
		test: /\.css$/,
		exclude: /node_modules/,
		loader: 'style-loader!css-loader'
	},
	{
		test: /\.js?$/, // Match both .js and .jsx files
		exclude: /node_modules/,
		loader: "jsx-loader"
	}
];

//通过src配置项，替换成min的filename，生成min配置项
var getMinConfig = function(conf,filename){
	var min = {};
	for(var key in conf){
		if(key == 'filename'){
			min[key] = filename;
		}else{
			min[key] = conf[key];
		}
	}
	return min;
}


//********CSS*************
var css_config = {
	entry: {
		'ucs.scss': './sass/ucs.scss'
	},
	output: {
		path: 'product/style',
		filename: 'ucs.css'
	},
	plugins: [
		new ExtractTextPlugin("ucs.css")
	],
	module: {
		loaders: [{
				test: /\.eot|ttf|svg|png|gif|woff2?$/,
				exclude: /node_modules/,
				loader: 'url',
				query: {
					limit: 10240,
					name: '../content/images/[name].[ext]'
				}
			},
			{
				test: /\.scss$/,
				exclude: /node_modules/,
				//loader:'style-loader!css-loader!sass-loader'
				loader: ExtractTextPlugin.extract('style', 'css!sass')
			}
		]
	}
}

//********UCSMY-UI*************
var ucsmy_ui_cfg = {
	entry:{
		ENTRY_FILE_NAME: SOURCE_DIR + '/' + ENTRY_FILE_NAME,
	},
	output: {
		path: PRODUCT_DIR,
		filename: 'ucsmy-ui.js',
		libraryTarget: 'umd',
		library: LIBRARY_NAME
	}
}

var ucsmy_ui_src_config = {
	entry: ucsmy_ui_cfg.entry,
	output: ucsmy_ui_cfg.output,
	module: {
		loaders: common_loaders
	}
}

var ucsmy_ui_min_config = {
	entry: ucsmy_ui_cfg.entry,
	output: getMinConfig(ucsmy_ui_cfg.output,'ucsmy-ui.min.js'),
	plugins: [
		new UglifyJsPlugin({ //压缩代码
			compress: {
				warnings: false
			},
			output: {
				comments: false
			},
			except: ['$super', '$', 'exports', 'require'] //排除关键字
		})
	],
	module: {
		loaders: common_loaders
	}
}

//********ENGINE*************
var engine_cfg = {
	entry: {
		'engine.js': './src/core/index.js',
	},
	output:{
		path: PRODUCT_DIR + '/libs',
		filename: 'engine.js',
		libraryTarget: 'umd',
		library: 'React'
	}	
}

var engine_src_config = {
	entry: engine_cfg.entry,
	output: engine_cfg.output,
	module: {
		loaders: common_loaders
	}
}

var engine_min_config = {
	entry: engine_cfg.entry,
	output: getMinConfig(engine_cfg.output,'engine.min.js'),
	plugins: [
		new UglifyJsPlugin({ //压缩代码
			compress: {
				warnings: false
			},
			output: {
				comments: false
			},
			except: ['$super', '$', 'exports', 'require'] //排除关键字
		})
	],
	module: {
		loaders: common_loaders
	}
}

//********U-EVENTHUB*************
var u_eventhub_cfg = {
	entry: {
		'u-eventhub.js': './src/utils/u-eventhub/index.js',
	},
	output:{
		path: PRODUCT_DIR + '/libs',
		filename: 'u-eventhub.js',
		libraryTarget: 'umd',
		library: 'UEventHub'
	}
};

var u_eventhub_src_config = {
	entry: u_eventhub_cfg.entry,
	output:u_eventhub_cfg.output
}

var u_eventhub_min_config = {
	entry: u_eventhub_cfg.entry,
	output: getMinConfig(u_eventhub_cfg.output,'u-eventhub.min.js'),
	plugins: [
		new UglifyJsPlugin({ //压缩代码
			compress: {
				warnings: false
			},
			output: {
				comments: false
			},
			except: ['$super', '$', 'exports', 'require'] //排除关键字
		})
	]
}

//********U-AJAX*************
var u_ajax_cfg = {
	entry: {
		'u-ajax.js': './src/utils/u-ajax/index.js',
	},
	output: {
		path: PRODUCT_DIR + '/libs',
		filename: 'u-ajax.js',
		libraryTarget: 'umd',
		library: 'UAjax'
	}
};

var u_ajax_src_config = {
	entry: u_ajax_cfg.entry,
	output: u_ajax_cfg.output
}

var u_ajax_min_config = {
	entry: u_ajax_cfg.entry,
	output: getMinConfig(u_ajax_cfg.output,'u-ajax.min.js'),
	plugins: [
		new UglifyJsPlugin({ //压缩代码
			compress: {
				warnings: false
			},
			output: {
				comments: false
			},
			except: ['$super', '$', 'exports', 'require'] //排除关键字
		})
	]
}

module.exports = [ucsmy_ui_src_config, ucsmy_ui_min_config,
	engine_src_config, engine_min_config,
	u_eventhub_src_config, u_eventhub_min_config,
	u_ajax_src_config, u_ajax_min_config,
	css_config
];